﻿Public Class NetGui
    Public StrFile As String
    Dim strFileName As String
    Public StrPath As String
    Public StrLogFile As String
Private Sub BtnGetHostNameClick( sender As Object,  e As EventArgs) Handles btnGetHostName.Click
    Dim strIP As String
    Dim iPResult As System.Net.IPHostEntry
    Dim netTools As new NetworkTools
    strIP = txtIP.text
    iPResult = netTools.GetHostNameFromIP(strIP)
    MessageBox.Show(iPResult.HostName)
End Sub

Private Sub BtnPingClick( sender As Object,  e As EventArgs) Handles BtnGetInfo.Click
    Dim strResult, strIP As String
    Dim iPResult As System.Net.IPHostEntry
    Dim netTools As new NetworkTools
    strIP = txtIP.text
    strResult = netTools.IsPingable(strIP)
    While Not pingFinished = True
        MessageBox.Show("Ping is finishing this may take a moment")
        Console.WriteLine("Ping is closing threads")
    End While
    LblPingIP.Text = "LblPingIP: " & strResult
    'iPResult = netTools.GetHostNameFromIP(strResult)
    If netTools.IsIpValid(strResult) = True Then
        Try
            iPResult = netTools.GetHostNameFromIP(strResult)
            LblSingleResultDHost.Text = "DNSHostName: " & iPResult.HostName
            LblSingleResultDIP.Text = "DNSIPAddress: "  & iPResult.AddressList(0).ToString
        Catch ex As Exception
            LblSingleResultDHost.Text = "No Host Name"
            LblSingleResultDIP.Text = "No valid IP"
        End Try
    ElseIf netTools.IsIpValid(strResult) = False Then
        LblSingleResultDHost.Text = "No Host Name"
        LblSingleResultDIP.Text = "No valid IP"
    End If
    
    'LblSingleResultDHost.Text = "DNSHostName: " & iPResult.HostName
End Sub

Private Sub BtnOpenFileClick( sender As Object,  e As EventArgs) Handles btnOpenFile.Click
        OpenFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
        OpenFileDialog1.Title = "Select Excel File"
        OpenFileDialog1.Filter = "Excel Files|*.xls"
        OpenFileDialog1.FileName = ""
        OpenFileDialog1.ShowDialog()
        lblFilePath.Text = "File: " & OpenFileDialog1.FileName
        StrFilePath = OpenFileDialog1.FileName
        Dim intPath As Integer
        
        'Calls Sub to clear datatables
        ClearData()
        
        If StrFilePath <> "" Then
            'btnValidate.Enabled = True
            'objEx.StrFile = StrFile
            'ExWs = objEx.OpenExcel()
            'intRecordCount = mfl.RecordCount(StrFile)
            intPath = InStrRev(StrFilePath, "\")
            StrPath = Microsoft.VisualBasic.Left(StrFilePath, intPath)
            'MsgBox(StrPath)
            strFileName = Microsoft.VisualBasic.Right(StrFilePath, StrFilePath.Length - intPath)
            'StrLogFile = StrPath & "PCRename_ERR_LOG-" & DtBuild & ".log"
            ModGlobals.StrLogFile = StrLogFile
            ModGlobals.StrFilePath = StrFilePath
            ModGlobals.strPath = StrPath
            Dim exTools As New ExcelTools
            Dim aTable As datatable
            aTable = exTools.LoadData()
            DataGridView1.DataSource = aTable
            AddColumns()
            LblHostNameCount.Text = "Num of HostNames: " & DataGridView1.Rows.Count()-1
            BtnProcess.Enabled = True
            BtnExport.Enabled = False
            ProgressBar1.Maximum = DataGridView1.Rows.Count()-1
            'DataGridView1.Columns.Clear()
        End If
        '
End Sub

    Private Sub BtnProcessClick(sender As System.Object, e As System.EventArgs) Handles BtnProcess.Click
        PingShiz()
        pingFinished = False
        DataGridView1.DataSource = pingResultTbl
        While Not pingFinished = True
            MessageBox.Show("Ping is finishing this may take a moment")
            Console.WriteLine("Ping is closing threads")
        End While
        DNsShiz()
        BtnExport.Enabled = True
        'AddColumns()
    End Sub
    ' Configures columns for the DataGridView control.
    Private Sub AddColumns()

        'Dim PIPAddress As New DataGridViewTextBoxColumn()
        'PIPAddress.HeaderText = "PIPAddress"
        'PIPAddress.Name = "PIPAddress"

        Dim dnsHostName As New DataGridViewTextBoxColumn()
        DNSHostName.HeaderText = "DNSHostName"
        DNSHostName.Name = "DNSHostName"

        Dim dnsIPAddress As New DataGridViewTextBoxColumn()
        DNSIPAddress.HeaderText = "DNSIPAddress"
        DNSIPAddress.Name = "DNSIPAddress"

        'Adds each column
        'DataGridView1.Columns.Add(PIPAddress)
        DataGridView1.Columns.Add(DNSHostName)
        DataGridView1.Columns.Add(DNSIPAddress)

    End Sub
    Sub ClearData()
        DataGridView1.DataSource = Nothing
        DataGridView1.Columns.Clear
        DataGridView1.Rows.Clear
        PingResultTbl.Columns.Clear
        PingResultTbl.Rows.Clear
    End Sub
    Public Sub PingShiz()
        Dim netTools As New NetworkTools
        Dim intRow As Integer = 0
        Dim rHost As String
        netTools.BuildTable()
        Console.WriteLine("Data Table Built")
        For Each host As DataGridViewRow In DataGridView1.Rows
            If Not Host.IsNewRow Then

                rHost = Host.Cells(0).Value.ToString
                netTools.IsPingable(rHost)
                DataGridView1(1, intRow).Value = strPResult

            End If
            intRow = intRow + 1
        Next
        DataGridView1.Refresh()
    End Sub
    Public Sub DNsShiz()
        Dim netTools As New NetworkTools
        Dim iPResult As New System.Net.IPHostEntry
        Dim intRow As Integer = 0
        Dim rIP As String
        'Dim boolCheck As Boolean
        For Each iP As DataGridViewRow In DataGridView1.Rows
            If Not IP.IsNewRow Then
                rIP = IP.Cells(3).Value.ToString
                'MsgBox(rIP)
                If netTools.IsIpValid(rIP) = True Then
                    Try
                        iPResult = netTools.GetHostNameFromIP(rIP)
                        DataGridView1(0, intRow).Value = iPResult.HostName
                        DataGridView1(1, intRow).Value = iPResult.AddressList(0)
                    Catch ex As Exception
                        DataGridView1(0, intRow).Value = "No Host Name"
                        DataGridView1(1, intRow).Value = "No valid IP"
                    End Try
                ElseIf netTools.IsIpValid(rIP) = False Then
                    DataGridView1(0, intRow).Value = "No Host Name"
                    DataGridView1(1, intRow).Value = "No valid IP"
                End If
            End If
            intRow = intRow + 1
        Next
        DataGridView1.Refresh()
    End Sub
    Private Sub BtnExportClick(sender As System.Object, e As System.EventArgs) Handles BtnExport.Click
        BtnProcess.Enabled = False
        Dim exTools As New ExcelTools
        exTools.Export2Excel()
    End Sub
End Class